package excel

import "github.com/tealeg/xlsx"

func Create(filename string, head []string) {
	file := xlsx.NewFile()

	sheet, err := file.AddSheet("sheet1")
	if err != nil {
		panic(err.Error())
	}

	style := xlsx.NewStyle()
	style.Alignment = xlsx.Alignment{
		Horizontal: "center",
		Vertical:   "center",
	}
	//增加表头行
	row := sheet.AddRow()
	for _, item := range head {
		cell := row.AddCell()
		cell.Value = item
		cell.SetStyle(style)
	}

	if err := file.Save(filename + ".xlsx"); err != nil {
		panic(err.Error())
	}
}

func AddContentByRow(filename string, content []string) {
	file, err := xlsx.OpenFile(filename + ".xlsx")
	if err != nil {
		panic(err.Error())
	}

	row := file.Sheets[0].AddRow()
	for _, item := range content {
		cell := row.AddCell()
		cell.Value = item
	}

	if err := file.Save(filename + ".xlsx"); err != nil {
		panic(err.Error())
	}
}
